package com.atguigu.com.nio.nio_5_零拷贝;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author songyahui
 * @Date 2023/10/29 22:07
 * @Description
 */
public class NewIOClient {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 8001));
        String filename = "test.log";
//        得到一个文件channel
        FileChannel fileChannel = new FileInputStream(filename).getChannel();
//        准备发送
        long startTime = System.currentTimeMillis();
//        在linux下一个transferTo方法就可以完成传输
//        在windows下一次调用transferTo只能发送8m, 就需要分段传输，而且要主要传输时候都位置
//        transferTo 底层使用零拷贝
        long transferCount = fileChannel.transferTo(0,fileChannel.size(), socketChannel);
        System.out.println("发送到总字节数 = "+transferCount+"耗时："+(System.currentTimeMillis() - startTime));
//        关闭资源
        fileChannel.close();
    }
}
